import com.sun.org.apache.bcel.internal.generic.Select

import java.nio.channels.SelectionKey
import java.nio.channels.Selector
import java.nio.channels.ServerSocketChannel
import java.nio.channels.SocketChannel
import java.util.concurrent.TimeUnit

/**
 * Created by sunlei on 2015/8/25.
 */

ServerSocketChannel sschannel=ServerSocketChannel.open()
ServerSocket serverSocket=sschannel.socket()
serverSocket.bind(new InetAddress(1024))
sschannel.configureBlocking(false)

Selector selector=Selector.open();
SelectionKey selectionKey=sschannel.register(selector,SelectionKey.OP_ACCEPT)
while(true){
    int cnt=selector.select(10,TimeUnit.MICROSECONDS)
    if(cnt<=0){
        continue
    }

    Iterator<SelectionKey> it=selector.selectedKeys().iterator()
    while(it.hasNext()){
        SelectionKey key=(SelectionKey)it.next();
        if(key.isAcceptable()){
            ServerSocketChannel serverSocketChannel=key.channel()
            SocketChannel socketChannel=serverSocketChannel.accept()
            socketChannel.configureBlocking(false)
            socketChannel.register(selector,SelectionKey.OP_READ)
        }
    }
}